<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button>点击</button>

</body>
<script>
  // http({
  //   url: "https://meituan.thexxdd.cn/api/frontpage",
  //   method: "GET"
  // })
  let button = document.querySelector("button")
  button.addEventListener("click", async function () {
    let result = await http({
      url: "https://meituan.thexxdd.cn/api/frontpage",
      method: "GET"
    }).catch(err => console.error(err))
    console.log(result)
  })


  function http({ url, method = "GET" }) {

    return new Promise((resolve, reject) => {

      let xhr = new XMLHttpRequest()
      xhr.open(method, url)

      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
          if (xhr.status >= 200 && xhr.status < 300) {
            // console.log(JSON.parse(xhr.response))
            resolve(JSON.parse(xhr.response))
          } else {
            reject(Error("出错啦"))
          }
        }
      }
      xhr.send()
    })



  }


</script>

</html>